package com.wanxi.tool.service;

import com.wanxi.tool.domain.ImgUpload;
import com.wanxi.tool.api.ToolFeign;
import com.wanxi.tool.domain.result.ToolResult;
import com.wanxi.tool.utils.ToolUtil;
import com.wanxi.tool.utils.UploadUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @Author:Iny
 * @Data：2021/12/28 9:14
 */
@RestController
@RequestMapping("back/tool")
@SuppressWarnings("all")//去掉所有警告
public class ToolServiceImpl implements ToolFeign {
    @Resource
    JavaMailSender javaMailSender;

    @Value("${email.sender}")
    String sender;

    @Resource
    private RedisTemplate redisTemplate;

    @ApiOperation(value="图片上传接口")
    @ApiImplicitParam(name = "file",value = "图片文件",dataType = "MultipartFile",required = true)
    @PostMapping("uploadImg")
    public ToolResult<ImgUpload> uploadImg(MultipartFile file, HttpServletRequest req){
        ImgUpload imgUpload = UploadUtil.uploadFile(file, req);
        return ToolResult.success();
    }

    @ApiOperation(value="邮件发送接口")
    @ApiImplicitParam(name = "email",value = "需要接收验证码的邮箱",dataType = "String",required = true)
    @GetMapping("getCodeByEmail")
    public ToolResult<String> getEmail(String email){
        // 构建一个邮件对象
        SimpleMailMessage message = new SimpleMailMessage();
        // 设置邮件主题
        message.setSubject("验证码");
        // 设置邮件发送者，这个跟application.yml中设置的要一致
        message.setFrom(sender);
        // 设置邮件接收者，可以有多个接收者，中间用逗号隔开，以下类似
        // message.setTo("10*****16@qq.com","12****32*qq.com");
        message.setTo(email);
        // 设置邮件发送日期
        message.setSentDate(new Date());

        String code = ToolUtil.getCode(6);
//        将验证码添加到redis中 过期时间5分钟
        redisTemplate.opsForValue().set(email,code,60*5, TimeUnit.SECONDS);
        // 设置邮件的正文
        message.setText("验证码："+ code);
        // 发送邮件
        javaMailSender.send(message);
        return ToolResult.success("邮件发送成功！有效时间60s");
    }

    @ApiOperation(value="验证码校验")
    @ApiImplicitParam(name = "code",value = "验证码",dataType = "String",required = true)
    @GetMapping("getCode")
    public ToolResult<String> getCode(String email){
        String code = (String)redisTemplate.opsForValue().get(email);
        if(code!=null){
            return ToolResult.success(code);
        }
        return ToolResult.failed();
    }
}
